namespace cnVery.Cms
{
    using cnVery.Data;
    using cnVery.Utils;
    using System;
    using System.Data;
    using System.Text;

    public class products
    {
        public static void Add(product model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into products(");
            builder.Append("[categoryid],[code],[name],[price],[money],[brand],[store],[keywords],[discount],[issuedate],[clickcount],[saledcount],[description],[detaildescription],[downurl],[downdescription],[smallimage],[bigimage],[vouch],[visible],[deleted],[mode],[sid]");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.categoryid + ",");
            builder.Append("'" + model.code + "',");
            builder.Append("'" + model.name + "',");
            builder.Append("" + model.price + ",");
            builder.Append("" + model.money + ",");
            builder.Append("'" + model.brand + "',");
            builder.Append("'" + model.store + "',");
            builder.Append("'" + model.keywords + "',");
            builder.Append("" + model.discount + ",");
            builder.Append("'" + model.issuedate + "',");
            builder.Append("" + model.clickcount + ",");
            builder.Append("" + model.saledcount + ",");
            builder.Append("'" + model.description + "',");
            builder.Append("'" + model.detaildescription + "',");
            builder.Append("'" + model.downurl + "',");
            builder.Append("'" + model.downdescription + "',");
            builder.Append("'" + model.smallimage + "',");
            builder.Append("'" + model.bigimage + "',");
            builder.Append("" + (model.vouch ? 1 : 0) + ",");
            builder.Append("" + (model.visible ? 1 : 0) + ",");
            builder.Append("" + (model.deleted ? 1 : 0) + ",");
            builder.Append("" + model.mode + ",");
            builder.Append("" + model.sid + "");            //Patch 0.1
            builder.Append(")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Delete(product model)
        {
            Tools.deletefiles(model.detaildescription);
            Tools.deletefile(model.smallimage);
            Tools.deletefile(model.bigimage);
        }

        public static void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete products ");
            builder.Append(" where id=" + id);
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from products where id=" + id + "");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return false;
            }
            int num = (int) scalar;
            return (num > 0);
        }

        public static int GetCount(string filter)
        {
            if (filter == "")
            {
                filter = "0=0";
            }
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from products where " + filter);
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 0;
            }
            return (int) scalar;
        }

        public static DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from products ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by id desc");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static DataTable GetList(int pageindex, int pagesize, string filter)
        {
            if (pageindex <= 0)
            {
                return null;
            }
            if (filter == "")
            {
                filter = "0=0";
            }
            string sql = string.Concat(new object[] { "select top ", pagesize, " * from products where ", filter });
            if (pageindex > 1)
            {
                object obj2 = sql;
                sql = string.Concat(new object[] { obj2, " and id < (select min(id) from (select top ", pagesize * (pageindex - 1), " id from products where ", filter, " order by id desc) as tb)" });
            }
            sql = sql + " order by id desc ";
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(sql);
            connection.Close();
            return dataTable;
        }

        public static product GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from products ");
            builder.Append(" where id=" + id);
            product product = new product();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            product.id = id;
            if (dataTable.Rows.Count <= 0)
            {
                return null;
            }
            if (dataTable.Rows[0]["categoryid"].ToString() != "")
            {
                product.categoryid = int.Parse(dataTable.Rows[0]["categoryid"].ToString());
            }
            product.code = dataTable.Rows[0]["code"].ToString();
            product.name = dataTable.Rows[0]["name"].ToString();
            if (dataTable.Rows[0]["price"].ToString() != "")
            {
                product.price = decimal.Parse(dataTable.Rows[0]["price"].ToString());
            }
            if (dataTable.Rows[0]["money"].ToString() != "")
            {
                product.money = int.Parse(dataTable.Rows[0]["money"].ToString());
            }
            if (dataTable.Rows[0]["mode"].ToString() != "")
            {
                product.mode = int.Parse(dataTable.Rows[0]["mode"].ToString());
            }
            product.store = dataTable.Rows[0]["store"].ToString();
            product.brand = dataTable.Rows[0]["brand"].ToString();
            product.keywords = dataTable.Rows[0]["keywords"].ToString();
            if (dataTable.Rows[0]["discount"].ToString() != "")
            {
                product.discount = decimal.Parse(dataTable.Rows[0]["discount"].ToString());
            }
            if (dataTable.Rows[0]["issuedate"].ToString() != "")
            {
                product.issuedate = DateTime.Parse(dataTable.Rows[0]["issuedate"].ToString());
            }
            if (dataTable.Rows[0]["clickcount"].ToString() != "")
            {
                product.clickcount = int.Parse(dataTable.Rows[0]["clickcount"].ToString());
            }
            if (dataTable.Rows[0]["saledcount"].ToString() != "")
            {
                product.saledcount = int.Parse(dataTable.Rows[0]["saledcount"].ToString());
            }
            //Patch 0.1
            if (dataTable.Rows[0]["sid"].ToString() != "")
            {
                product.sid = int.Parse(dataTable.Rows[0]["sid"].ToString());
            }
            product.description = dataTable.Rows[0]["description"].ToString();
            product.detaildescription = dataTable.Rows[0]["detaildescription"].ToString();
            product.downurl = dataTable.Rows[0]["downurl"].ToString();
            product.downdescription = dataTable.Rows[0]["downdescription"].ToString();
            product.smallimage = dataTable.Rows[0]["smallimage"].ToString();
            product.bigimage = dataTable.Rows[0]["bigimage"].ToString();
            if (dataTable.Rows[0]["vouch"].ToString() != "")
            {
                if ((dataTable.Rows[0]["vouch"].ToString() == "1") || (dataTable.Rows[0]["vouch"].ToString().ToLower() == "true"))
                {
                    product.vouch = true;
                }
                else
                {
                    product.vouch = false;
                }
            }
            if (dataTable.Rows[0]["visible"].ToString() != "")
            {
                if ((dataTable.Rows[0]["visible"].ToString() == "1") || (dataTable.Rows[0]["visible"].ToString().ToLower() == "true"))
                {
                    product.visible = true;
                }
                else
                {
                    product.visible = false;
                }
            }
            if (dataTable.Rows[0]["deleted"].ToString() != "")
            {
                if ((dataTable.Rows[0]["deleted"].ToString() == "1") || (dataTable.Rows[0]["deleted"].ToString().ToLower() == "true"))
                {
                    product.deleted = true;
                    return product;
                }
                product.deleted = false;
            }
            return product;
        }

        public static void Update(product model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update products set ");
            builder.Append("[categoryid]=" + model.categoryid + ",");
            builder.Append("[code]='" + model.code + "',");
            builder.Append("[name]='" + model.name + "',");
            builder.Append("[price]=" + model.price + ",");
            builder.Append("[money]=" + model.money + ",");
            builder.Append("[brand]='" + model.brand + "',");
            builder.Append("[store]='" + model.store + "',");
            builder.Append("[keywords]='" + model.keywords + "',");
            builder.Append("[discount]=" + model.discount + ",");
            builder.Append("[issuedate]='" + model.issuedate + "',");
            builder.Append("[clickcount]=" + model.clickcount + ",");
            builder.Append("[saledcount]=" + model.saledcount + ",");
            //Patch 0.1
            builder.Append("[sid]=" + model.sid + ",");
            builder.Append("[description]='" + model.description + "',");
            builder.Append("[detaildescription]='" + model.detaildescription + "',");
            builder.Append("[downurl]='" + model.downurl + "',");
            builder.Append("[downdescription]='" + model.downdescription + "',");
            builder.Append("[smallimage]='" + model.smallimage + "',");
            builder.Append("[bigimage]='" + model.bigimage + "',");
            builder.Append("[vouch]=" + (model.vouch ? 1 : 0) + ",");
            builder.Append("[mode]=" + model.mode + ",");
            builder.Append("[visible]=" + (model.visible ? 1 : 0) + ",");
            builder.Append("[deleted]=" + (model.deleted ? 1 : 0) + "");
            builder.Append(" where id=" + model.id + "");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Update(string sql, string ids)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update [products] set " + sql + " where id in (" + ids + ")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }
    }
}

